package org.csu.hotelsys.controller;


import org.csu.hotelsys.domain.Check_in;
import org.csu.hotelsys.domain.Customer;
import org.csu.hotelsys.domain.Room;
import org.csu.hotelsys.domain.Room_Reserve;
import org.csu.hotelsys.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/customer/")
public class CustomerController {
    @Autowired
    CustomerService customerService;



    //进入用户管理界面
    @GetMapping("main")
    public String control(){
        return "customer/main";
    }

/*-------------------------------------------------预定管理----------------------------*/
    //预定管理主界面,包含预定以及修改预定信息模块
    @GetMapping("reserveMain")
    public String reserveMain(Model model){
        List<Room_Reserve> room_reserveList=customerService.getAllReserveRoomList();
        if(room_reserveList.isEmpty()){
        }else{
            model.addAttribute("room_reserveList",room_reserveList);
        }
        return "customer/reserveMain";
    }


    //取消预定
    @GetMapping("cancelReserve")
    public String cancelReserve(String reserver_id,Model model){
        //原本预定信息获取
        Room_Reserve room_reserve=customerService.getReserveRoomByID(reserver_id);
        model.addAttribute("room_reserve",room_reserve);
        String msg="成功取消订单";
        model.addAttribute("msg",msg);
        List<Room_Reserve> room_reserveList=new ArrayList<>();
        room_reserveList.add(room_reserve);
        model.addAttribute("room_reserveList",room_reserveList);
        return "customer/reserveMain";
    }


    //输入预定信息界面
    @GetMapping("addReserve")
    public String reserveRoom(Model model){
        List<Room> roomList=customerService.getavailableRoomList("0");//0代表空闲，获取可预订房间列表
        model.addAttribute("roomList",roomList);
        return "customer/addReserve";
    }

    //提交预定信息并返回预定主界面
    @PostMapping("addedReserve")
    public String postReserve(Room_Reserve room_reserve,Model model){
        customerService.reserveRoom(room_reserve);//客房预订
        room_reserve=customerService.getReserveRoomByID(room_reserve.getReserver_id());
        List<Room_Reserve> room_reserveList=new ArrayList<>();
        room_reserveList.add(room_reserve);
        model.addAttribute("room_reserveList",room_reserveList);
        String msg="成功新建订单";
        model.addAttribute("msg",msg);
        return "customer/reserveMain";
    }



    //修改或注销预定信息界面,界面信息为所有未取消未入住预定单
    @GetMapping("changeReserve")
    public String changeReserve(String reserver_id,Model model){
        List<Room> availableRoomList=customerService.getavailableRoomList("0");//0代表空闲，获取可预订房间列表
        model.addAttribute("roomList",availableRoomList);

        //原本预定信息获取
        Room_Reserve room_reserve=customerService.getReserveRoomByID(reserver_id);
        model.addAttribute("room_reserve",room_reserve);
        return "customer/changeReserve";
    }


    //提交修改信息
    @PostMapping("changedReserve")
    public String postChangeReserve(Room_Reserve room_reserve,Model model){
        String msg="成功修改信息";
        model.addAttribute("msg",msg);
        customerService.updateReserveRoom(room_reserve);
        List<Room_Reserve> room_reserveList=new ArrayList<>();
        room_reserveList.add(room_reserve);
        model.addAttribute("room_reserveList",room_reserveList);
        return "customer/reserveMain";
    }


    //搜索栏，根据用户电话搜索预定信息,返回符合该电话的所有未取消未入住预定单
    @PostMapping("searchByPhone")
    public String searchByphone(String phone,Model model){
        List <Room_Reserve> room_reservesList=customerService.getReserveRoomByPhone(phone);
        if(room_reservesList.isEmpty()){
            String msg="查无订单";
            model.addAttribute("msg",msg);
        }else{
            model.addAttribute("room_reservesList",room_reservesList);
        }
        return "customer/reserveMain";
    }


    /*-------------------------------------------------入住管理----------------------------*/
    //进入入住管理界面,该界面显示所有入住订单信息,以及新增入住按钮
    @GetMapping("InRoomMain")
    public String InRoomMain(Model model){
        List<Check_in> check_inList=customerService.getCheck_inList();
        model.addAttribute("check_inList",check_inList);
        return "customer/InRoomMain";
    }

    //预定转入住
    @PostMapping("reserveInRoom")
    public String reserveInRoom(String reserver_id,Model model){
        String msg;
        msg="成功入住";
        model.addAttribute("msg",msg);
        Check_in check_in=customerService.reserveInRoom(reserver_id);
        List<Check_in> check_inList=new ArrayList<>();
        check_inList.add(check_in);
        model.addAttribute("check_inList",check_inList);
        return "customer/InRoomMain";
    }

    //直接入住按钮，进入新增入住界面
    @GetMapping("directInRoom")
    public String directInRoom(Model model){

        List<Room> availableRoomList=customerService.getavailableRoomList("0");//0代表空闲，获取可预订房间列表
        model.addAttribute("availableRoomList",availableRoomList);


        return "customer/directInRoom";
    }

    //填写入住信息并提交,返回入住主界面
    @PostMapping("directedInRoom")
    public String setDirectInRoom(Check_in check_in,Model model){
        String msg;
        msg="成功入住";
        model.addAttribute("msg",msg);
        customerService.inRoom(check_in);

        check_in=customerService.getCheck_inById(check_in.getCheck_id());
        List<Check_in> check_inList=new ArrayList<>();
        check_inList.add(check_in);

        model.addAttribute("check_inList",check_inList);
        return "customer/InRoomMain";
    }




    //感觉身份证件查找入住单,因为一个身份证可能对应N个入住单，故一开始近返回列表表格
    @GetMapping("searchInRoom")
    public String searchInRoom(String credentials_num,Model model){
        List<Check_in> check_inList=customerService.getCheck_inByCredit(credentials_num);
        if(check_inList.isEmpty()){
            String msg;
            msg="查无信息";
            model.addAttribute("msg",msg);
        }else{
            model.addAttribute("check_inList",check_inList);
        }
        return "customer/InRoomMain";
    }

    //打印入住单
    @GetMapping("printInRoom")
    public String printInRoom(String check_id,Model model){
        Check_in check_in=customerService.getCheck_inById(check_id);

        return "customer/printInRoom";
    }

    /*-------------------------------------------------换房管理----------------------------*/
    @GetMapping("changeRoomMain")
    public String changeRoomMain(Model model){
        //获取所有为退房的入住名单
        List<Check_in> check_inList=customerService.getNoExitCheck_inList();
        model.addAttribute("check_inList",check_inList);
        return "customer/changeRoomMain";
    }

    //根据身证号查找入住信息
    @PostMapping("getCheckIn")
    public String getCheckIn(String credentials_num,Model model){
        List<Check_in> check_inList=customerService.getCheck_inByCredit(credentials_num);
        String msg;
        if(check_inList.isEmpty()){
            msg="没有入住信息,请核对后输入";
        }else {
            model.addAttribute("check_inList",check_inList);
        }
        return "customer/changeRoomMain";
    }


    @GetMapping("changeRoom")
    public  String changeRoom(String check_id,Model model){
        //获取空闲房间
        List<Room> availableRoomList=customerService.getavailableRoomList("0");//0代表空闲，获取可更换房间列表
        model.addAttribute("availableRoomList",availableRoomList);
        Check_in check_in=customerService.getCheck_inById(check_id);
        model.addAttribute(check_in);
        return "customer/changeRoom";
    }


    //Post换房信息
    @PostMapping("changedRoom")
    public String changedRoom(Check_in check_in,Model model){
        String msg;
        msg="换房成功";
        customerService.updateCheck_inRoom(check_in);
        List<Check_in> check_inList=new ArrayList<>();
        check_inList.add(check_in);
        model.addAttribute("check_inList",check_inList);
        return "customer/changeRoomMain";
    }



    /*-------------------------------------------------退房管理----------------------------*/
    //进入退房管理界面，里面包含Customer表格，
    @GetMapping("exitRoomMain")
    public String exitRoomMain(Model model){
        List<Check_in> check_inList =customerService.getNoExitCheck_inList();
        model.addAttribute("check_inList",check_inList);
        return "customer/exitRoomMain";
    }


    @PostMapping("searchCustomer")
    public String searchCustomer(String credentials_num,Model model){
        List<Check_in> check_inList =customerService.getCheck_inByCredit(credentials_num);
        if(check_inList.isEmpty()){
            String msg;
            msg="未查询到入住信息，请检查是否输入有误";
            List<Check_in> check_inList2 =customerService.getNoExitCheck_inList();
            model.addAttribute("check_inList",check_inList2);
        }else{
            model.addAttribute("check_inList",check_inList);

        }
        return "customer/exitRoom";
    }


    //确认退房
    @GetMapping("toeEitRoom")
    public String toeEitRoom(String check_in_id){

        Check_in check_in=customerService.getCheck_inById(check_in_id);
        customerService.exitRoom(check_in);

        //更新Customer数据
        Customer customer=customerService.getCustomerById(check_in.getCredentials_num());
        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        if(customer==null){
            customer.setCheck_in_name(check_in.getCheck_in_name());
            customer.setCredentials_type(check_in.getCredentials_type());
            customer.setCredentials_num(check_in.getCredentials_num());
            customer.setLast_checkin_time(formatter.format(date));
            customerService.addCustoemr(customer);
            return "customer/exitRoom";
        }else{
            customer.setLast_checkin_time(formatter.format(date));
            return "customer/exitRoom";
        }
    }




}
